package com.org.gameloft.GLIDER.util;

import com.org.gameloft.GLIDER.common.GLLogger;

import java.io.IOException;
import java.io.File;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Aug 6, 2007
 * Time: 4:23:49 PM
 * Revision: 0
 */
public class Util
{
    private static String appFolder=null;

    public static String getAppFolder()
    {
        if (appFolder == null)
        {
            try
            {
                appFolder = new File(".").getCanonicalPath();
            }
            catch (IOException e)
            {
                GLLogger.log(e);
                return ".";
            }
        }

        return appFolder;
    }

    public static void checkAssociations()
    {
        try
        {
            String extension = ".glp";
            String fileType = "GLIDERProject";
            String executable = "java -jar "+getAppFolder()+"/glider.jar";
            
            execCommand("cmd /c assoc " + extension + "=" + fileType);
            execCommand("cmd /c ftype " + fileType + "=\"" + executable + "\" \"%1\" %*");
        }
        catch (Exception e)
        {
            GLLogger.log(e);
        }
    }

    public static void execCommand(String cmd) throws IOException, InterruptedException
    {
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
    }

    public static String formatInt(int v, int size)
    {
        String s = ""+v;
        while (s.length()<size)
            s = "0"+s;

        return s;
    }
}
